<?php
namespace Home\Controller;
use Think\Controller;
header("Content-type:text/html;Charset=utf-8");
/*Ajax请求处理中心*/
class AjaxController extends Controller{
    /*验证邮箱是否可用*/
    public function mail_test(){
        $db=M('user_reg');
        $name['mail']=I('post.mail');
        $re=$db->where($name)->find();
		if(count($re)==0){
			echo 0;/*邮箱可用*/
		}else{
			echo 1;/*邮箱已注册*/
		};
    }
    /*上传数据到数据库*/
	public function register(){
        $db=M('user_reg');
        $name['mail']=I('post.mail');
        $re=$db->where($name)->find();
		if(count($re)==0){
			echo 0;/*邮箱可注册*/
			$userreg['username']=I('post.username');
			$password=I('post.password');
            $rand=rand(1000,9999);
			$userreg['password']=MD5($password.$rand);
            $userreg['mail']=$name['mail'];
			$userreg['reason']=I('post.instruct');
			$userreg['rand']=$rand;
            $db->add($userreg);
		}else{
			echo 1;/*账号已注册*/
		};
    }
    /*登录信息验证*/
	public function login(){
		$db=M('user_reg');
		$userreg['mail']=I('post.mail');
		$arr=$db->where($userreg)->find();
		$rand=$arr['rand'];
		$userreg['password']=MD5(I('post.password').$rand);
		$arr1=$db->where($userreg)->find();
		$re=count($arr1);
		if($re==0){
			echo 0;/*登录失败*/
		}elseif($arr1['is_admin']==0){
            $token=MD5(rand(1000,9999));
            $token1=MD5($token);
            cookie('token',$token,600);
            cookie('token1',$token1,600);
            session($token,$arr1['id']);
            session($token1,$arr1['mail']);
			echo 1;/*登录成功*/
				}elseif($arr1['is_admin']==1){
		    echo 2;
        }
	}
}